package com.loto.graphics;

import javax.swing.JPanel;


import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import com.loto.model.Combinaison;
import com.loto.model.CombinaisonModele;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

public class Statistiques extends JFrame implements ActionListener {


	private JPanel stat = new JPanel();
	private JComboBox mois = new JComboBox();
	private JComboBox annee = new JComboBox();
	private JLabel label = new JLabel("Choix de la période ");
	private JLabel filtre1 = new JLabel("Mois: ");
	private JLabel filtre2 = new JLabel("Année: ");
	private JButton valider = new JButton("Valider");
	private static Statistiques instance;
	private CombinaisonModele combinaisonService;
	private List<Combinaison> listecombinaison;
	private JPanel graphe;

	private Statistiques() throws IOException {
		combinaisonService = new CombinaisonModele();
		this.setTitle("Statistiques");
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.graphe = new JPanel();
		graphe.setBackground(Color.white);
		graphe.setLayout(new BorderLayout());
		valider.addActionListener(this);

		mois.setPreferredSize(new Dimension(50, 20));
		for (int i = 1; i <= 12; i++) {
			mois.addItem(i);
		}
		annee.setPreferredSize(new Dimension(70, 20));
		for (int j = 2000; j <= 2012; j++) {
			annee.addItem(j);
		}

		JPanel top = new JPanel();
		top.add(label);

		top.add(filtre1);
		top.add(mois);
		top.add(filtre2);
		top.add(annee);
		top.add(valider);
		graphe.add(top, BorderLayout.NORTH);
		this.stat = this.affichagestat("2012/04/05");
		graphe.add(this.stat, BorderLayout.SOUTH);
		this.setContentPane(graphe);
		this.pack();
		this.setVisible(true);
	}

	private JPanel affichagestat(String date) {
		JPanel panel = new JPanel();
		this.listecombinaison = this.combinaisonService.getCombinaisonByDate(
				"2000/01/01", date);
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(int it1=0;it1<50;it1++ ){
			dataset.addValue(0, "Combinaison", new Integer(it1));
		}
		
		for (int i = 0; i < this.listecombinaison.size(); i++) {
			String str[] = this.listecombinaison.get(i).getCombinaisonGagnante().split("\\+");
			String combinaison[] = str[0].split("\\-");
			for (int it2=0; it2 < combinaison.length; it2++) {
				dataset.incrementValue(1, "Combinaison", new Integer(combinaison[it2]));
			}
		}
		JFreeChart chart = ChartFactory.createBarChart("Combinaisons ",
				"nombre", "Pourcentage", dataset, PlotOrientation.VERTICAL,
				true, true, false);
		ChartPanel cPanel = new ChartPanel(chart);
		panel.add(cPanel);
		return panel;
	}

	public void actionPerformed(ActionEvent e) {

		String mois;
		if (Integer.parseInt("" + this.mois.getSelectedItem()) < 10) {
			mois = "0" + this.mois.getSelectedItem();
		} else {
			mois = "" + this.mois.getSelectedItem();
		}
		String date = this.annee.getSelectedItem() + "/" + mois + "/01";
		this.graphe.remove(this.stat);
		this.stat =this.affichagestat(date);
		this.graphe.add(this.stat);
		this.graphe.repaint();
		this.graphe.revalidate();
		System.out.println("change");
	}

	public static Statistiques getInstance() throws IOException {
		if (instance == null) {
			instance = new Statistiques();
		}
		return instance;
	}

	public JPanel getStat() {
		return stat;
	}

	public JComboBox getMois() {
		return mois;
	}

	public JComboBox getAnnee() {
		return annee;
	}

	public JPanel getGraphe() {
		return graphe;
	}

}
